import { createStore } from 'vuex'
import persist from 'vuex-persistedstate'
export default createStore({
  state: {
    cartList:[], // 购物车的数据
  },
  mutations: {
    addCart(state,val){
      // console.log(val);
      // val.num = 1;
      let index = state.cartList.findIndex(item => item.id == val.id);
      // 上面是判断，加入的商品，是否在购物车里面已经存在，存在的话返回对应的下标，不存在返回的值是-1
      if (index == -1){
        state.cartList.push({...{ischeck:false,num:1},...val});
      } else {
        state.cartList[index].num++;
      }
    },
    checkAll(state,bool){
      state.cartList.forEach(item => {
        item.ischeck = bool;
      })
    },
    storeDel(state){
      state.cartList = state.cartList.filter(item => item.ischeck == false);
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ],
})
